var _table =null;
$(document).ready(function(){
    // 初始化表格
    initSysUserTable();
    
    $("#btn-advanced-search").click(function(){
		 refulsh();
	  });
});



var userManage = {
    getQueryCondition : function(data) {
        var param = {};

        param.userJobno = $("#userJobno").val();
        param.page = data.start;
        param.rows = data.length;

        return param;
    },
   
}


function data_edit(title,url,w,h){
	layer_show_callback(title,url,w,h,function(){
		refulsh();
	});
}

function data_del(obj,id){
	layer.confirm('确认要删除吗？',function(index){
		ajaxRequestJson("deleteSysUser",{
			"id":id
		},function(result){
			refulsh();
			layer.msg('已删除!', {icon: 6,time:1000});
		})
		
	});
}

function data_role(){
	layer_show_callback(title,url,w,h,function(){
		refulsh();
	});
}


function data_stop(obj,id){
	layer.confirm('确认要停用吗？',function(index){
		ajaxRequestJson("updateSysUser",{
			"id":id,
			"userStatus":102
		},function(result){
			refulsh();
			layer.msg('已停用!', {icon: 6,time:1000});
		})
		
	});
}

function data_start(obj,id){
	layer.confirm('确认要启用吗？',function(index){
		ajaxRequestJson("updateSysUser",{
			"id":id,
			"userStatus":101
		},function(result){
			refulsh();
			layer.msg('已启用!', {icon: 6,time:1000});
		})		
	});
}

function refulsh(){
	userManage.fuzzySearch = false;
    _table.draw();
}

//密码重置
function restPassWord(id){
	layer.confirm('确认要重置吗？',function(index){
		ajaxRequestJson("updateUserRetPassWord",{
			"id":id
		},function(result){
			refulsh();
			layer.msg('已重置!', {icon: 6,time:1000});
		})		
	});
}





function initSysUserTable(){
	  var $table = $('#table-user');
	    _table = $table.dataTable($.extend(true,{},CONSTANT.DATA_TABLES.DEFAULT_OPTION, {
		 orderMulti: false,
		 bFilter: false,
		 order: [],
		 renderer: "bootstrap",
		 pagingType: "simple_numbers",
		 aaSorting: [[ 1, "desc" ]],
		 bStateSave: true,
			pageLength:10,
			lengthMenu:[10,20,50,100],
			iDisplayLength: 10,
	        ajax : function(adata, callback, settings) {
	           var param = userManage.getQueryCondition(adata);
	            $.ajax({
	                    type: "post",
	                    url: "querySysUserPage",
	                    cache : false,
	                    data: param,
	                    dataType: "json",
	                    success: function(reslut) {
                            var returnData = {};
                            returnData.draw = adata.draw;
                            returnData.recordsTotal = reslut.data.total;
							returnData.recordsFiltered = reslut.data.total;
                            returnData.data = reslut.data.list;
							
							$("#total").text(reslut.data.total);
                            callback(returnData);
	                    },
	                    error: function(XMLHttpRequest, textStatus, errorThrown) {
	                       console.log("查询失败");
	                    }
	                });
	        },
    "columns": [
				 {"data":  function ( row, type, set ) {
		             return  '<input type="checkbox" class="iCheck" value='+row.id+'>';        
		         },"bSortable":false},
				{
					 "data": "userLoginname","bSortable": false
				},
				{
					 "data": "userJobno","bSortable": false
				},
				{
					 "data": "userPassword","bSortable": false
				},
				{
					 "data": "userPhone","bSortable": false
				},
				{
					 "data": "userEmail","bSortable": false
				},
//				{
//					 "data": "userType","bSortable": false
//				},
//				{
//					 "data": "loginPwdErrorNum","bSortable": false
//				},
				{
					 "data": "onlineis","bSortable": false
				},
				{
					 "data": "userStatusName","bSortable": false
				},
				{
					 "data": "creater","bSortable": false
				},
				{
					 "data": "gmtCreate","bSortable": false
				}
	    	
     ],
     "columnDefs": [    	
    	 {
             "targets": [6], 
             "data": "onlineis", 
             "render": function(data, type, full) { 
            	 if("1"==data){
            		return  '<span class="label label-success">在线</span>';
            	 }
                 return '<span class="label label-default">离线</span>';
             }
         },
    	 {
             "targets": [7],
             "data": "userStatus",
             "render": function(data, type, full) {
            	 if("102"==data){
            		return  '<span class="label label-default">'+full.userStatusName+'</span>';
            	 }
                 return '<span class="label label-success">'+full.userStatusName+'</span>';
             }
         },

         {
             "targets": [10],
             "data": "id",
             "render": function(data, type, full) { 
            	 var str ="";
            	 if("101"==full.userStatus){
            		 str ='<a style="text-decoration:none" onClick="data_stop(this,'+data+')" href="javascript:void(0);" title="禁用">';
            	 }else{
            		 str ='<a style="text-decoration:none" onClick="data_start(this,'+data+')" href="javascript:void(0);" title="启用">';
            	 }
            	 str =str+'<i class="Hui-iconfont">&#xe615;</i></a> <a title="编辑" href="javascript:void(0);" onclick="data_edit(\'管理员编辑\',\'initAddUpdateSysUserPage?id='+data+'\',\'500\',\'400\')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a> <a title="删除" href="javascript:void(0);" onclick="data_del(this,'+data+')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>';
            	 str = str+' <a title="分配角色" href="javascript:void(0);" onclick="data_edit(\'分配角色\',\''+$("#hiddenpath").val()+'/sysuserrole/initSysUserRole?uid='+data+'\',\'900\',\'700\')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe666;</i></a>';
            	 str = str+' <a title="密码重置" href="javascript:void(0);" onclick="restPassWord('+data+')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe63f</i></a>';
            	 
            	 return str;
             }
         }
     ]
	
	 })).api();   

};    
